﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace AyuBookmark.WinForm.Dialogs
{
    /// <summary>
    /// 设置对话框
    /// </summary>
    public partial class SettingsDialog : Form
    {
        // UI控件声明
        private TabControl tabControl;
        private TabPage tabGeneral;
        private TabPage tabSync;
        private TabPage tabPrivacy;
        private Button btnSave;
        private Button btnCancel;
        private CheckBox chkStartWithWindows;
        private CheckBox chkAutoSync;
        private NumericUpDown numSyncInterval;
        private ComboBox cmbTheme;
        private CheckBox chkUsageStatistics;
        private TextBox txtProxy;
        private Button btnResetSettings;

        public SettingsDialog()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            // 窗体基础设置
            this.SuspendLayout();
            this.Text = "设置";
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ClientSize = new Size(500, 400);
            this.BackColor = SystemColors.Window;

            // 选项卡控件
            this.tabControl = new TabControl();
            this.tabControl.Dock = DockStyle.Fill;
            this.tabControl.Appearance = TabAppearance.FlatButtons;

            // 常规设置选项卡
            this.tabGeneral = new TabPage("常规");
            this.tabGeneral.BackColor = SystemColors.Window;
            this.tabGeneral.Padding = new Padding(10);

            // 同步设置选项卡
            this.tabSync = new TabPage("同步");
            this.tabSync.BackColor = SystemColors.Window;
            this.tabSync.Padding = new Padding(10);

            // 隐私设置选项卡
            this.tabPrivacy = new TabPage("隐私");
            this.tabPrivacy.BackColor = SystemColors.Window;
            this.tabPrivacy.Padding = new Padding(10);

            // 按钮面板
            var panel = new Panel();
            panel.Dock = DockStyle.Bottom;
            panel.Height = 50;
            panel.BackColor = SystemColors.Control;

            // 保存按钮
            this.btnSave = new Button();
            this.btnSave.Text = "保存";
            this.btnSave.DialogResult = DialogResult.OK;
            this.btnSave.Size = new Size(100, 30);
            this.btnSave.Location = new Point(280, 10);
            this.btnSave.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // 取消按钮
            this.btnCancel = new Button();
            this.btnCancel.Text = "取消";
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Size = new Size(100, 30);
            this.btnCancel.Location = new Point(390, 10);
            this.btnCancel.Font = new Font("Microsoft YaHei", 9f);

            // 常规设置控件
            this.chkStartWithWindows = new CheckBox();
            this.chkStartWithWindows.Text = "开机自动启动";
            this.chkStartWithWindows.AutoSize = true;
            this.chkStartWithWindows.Location = new Point(20, 20);

            this.cmbTheme = new ComboBox();
            this.cmbTheme.Items.AddRange(new object[] { "浅色", "深色", "系统默认" });
            this.cmbTheme.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbTheme.Location = new Point(20, 60);
            this.cmbTheme.Size = new Size(150, 25);
            this.cmbTheme.Font = new Font("Microsoft YaHei", 9f);

            var lblTheme = new Label();
            lblTheme.Text = "主题设置:";
            lblTheme.AutoSize = true;
            lblTheme.Location = new Point(20, 40);

            // 同步设置控件
            this.chkAutoSync = new CheckBox();
            this.chkAutoSync.Text = "启用自动同步";
            this.chkAutoSync.AutoSize = true;
            this.chkAutoSync.Location = new Point(20, 20);

            this.numSyncInterval = new NumericUpDown();
            this.numSyncInterval.Minimum = 1;
            this.numSyncInterval.Maximum = 24;
            this.numSyncInterval.Value = 2;
            this.numSyncInterval.Location = new Point(20, 60);
            this.numSyncInterval.Size = new Size(60, 25);

            var lblSyncInterval = new Label();
            lblSyncInterval.Text = "同步间隔(小时):";
            lblSyncInterval.AutoSize = true;
            lblSyncInterval.Location = new Point(90, 63);

            this.txtProxy = new TextBox();
            this.txtProxy.Location = new Point(20, 100);
            this.txtProxy.Size = new Size(200, 25);
            this.txtProxy.Font = new Font("Microsoft YaHei", 9f);

            var lblProxy = new Label();
            lblProxy.Text = "代理服务器:";
            lblProxy.AutoSize = true;
            lblProxy.Location = new Point(20, 80);

            // 隐私设置控件
            this.chkUsageStatistics = new CheckBox();
            this.chkUsageStatistics.Text = "发送匿名使用统计";
            this.chkUsageStatistics.AutoSize = true;
            this.chkUsageStatistics.Location = new Point(20, 20);

            this.btnResetSettings = new Button();
            this.btnResetSettings.Text = "重置所有设置";
            this.btnResetSettings.Size = new Size(150, 30);
            this.btnResetSettings.Location = new Point(20, 60);
            this.btnResetSettings.Font = new Font("Microsoft YaHei", 9f);

            // 添加控件到选项卡
            this.tabGeneral.Controls.Add(this.chkStartWithWindows);
            this.tabGeneral.Controls.Add(lblTheme);
            this.tabGeneral.Controls.Add(this.cmbTheme);

            this.tabSync.Controls.Add(this.chkAutoSync);
            this.tabSync.Controls.Add(this.numSyncInterval);
            this.tabSync.Controls.Add(lblSyncInterval);
            this.tabSync.Controls.Add(lblProxy);
            this.tabSync.Controls.Add(this.txtProxy);

            this.tabPrivacy.Controls.Add(this.chkUsageStatistics);
            this.tabPrivacy.Controls.Add(this.btnResetSettings);

            // 添加选项卡
            this.tabControl.Controls.Add(this.tabGeneral);
            this.tabControl.Controls.Add(this.tabSync);
            this.tabControl.Controls.Add(this.tabPrivacy);

            // 添加按钮到面板
            panel.Controls.Add(this.btnSave);
            panel.Controls.Add(this.btnCancel);

            // 添加控件到窗体
            this.Controls.Add(this.tabControl);
            this.Controls.Add(panel);

            // 事件处理
            this.btnSave.Click += (s, e) => this.DialogResult = DialogResult.OK;
            this.btnCancel.Click += (s, e) => this.DialogResult = DialogResult.Cancel;
            this.btnResetSettings.Click += OnResetSettingsClick;

            this.ResumeLayout(false);
        }

        private void OnResetSettingsClick(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确定要重置所有设置吗？此操作不可撤销。",
                "确认重置",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {
                // 重置所有设置
                this.chkStartWithWindows.Checked = false;
                this.cmbTheme.SelectedIndex = 2;
                this.chkAutoSync.Checked = true;
                this.numSyncInterval.Value = 2;
                this.txtProxy.Text = "";
                this.chkUsageStatistics.Checked = false;
            }
        }

        public static DialogResult ShowSettingsDialog(IWin32Window owner)
        {
            using (var dialog = new SettingsDialog())
            {
                return dialog.ShowDialog(owner);
            }
        }
    }
}